package org.wu.framework.core.mapper;

import org.wu.framework.core.utils.DataTransformUntil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class LazyStructMappers {


    public static <T> T getMapper(Class<T> clazz) {
        ClassLoader classLoader = clazz.getClassLoader();
        Class<?>[] interfaces = new Class<?>[]{clazz};
        LazyMapperInvocationHandler lazyMapperInvocationHandler = new LazyMapperInvocationHandler();
        // 创建代理对象
        Object newProxyInstance = Proxy.newProxyInstance(classLoader, interfaces, lazyMapperInvocationHandler);
        return (T) newProxyInstance;
    }
}

class LazyMapperInvocationHandler implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // bean 转换
        Object source = args[0];
        Class<?> targetType = method.getReturnType();
        return DataTransformUntil.transformBean(source, targetType);

    }
}

